function EditPurchase(container, item, callbackFunc, options) {
  this._container = container;
  this._item = item;
  this._callbackFunc = callbackFunc;

  this._parent = (options && options.parent) ? options.parent : null;
  this._index = (options && options.index) ? options.index : null;
  this._samples  = (options && options.samples) ? options.samples : null;
  
  if (!this._item) {
    this._item = PurchasedSample.instance();
    this._item.sample = this._samples[0];
  }
  
  this._albums = [];
  for (var i = 0, il = this._parent.purchases.length; i < il; i++) {
    var purchase = this._parent.purchases[i];
    if (purchase.product.type == '{{$smarty.const.SAMPLE_TYPE_ALBUM|escape:javascript}}' || purchase.product.type == '{{$smarty.const.SAMPLE_TYPE_MAGAZINE_ALBUM|escape:javascript}}') {
      this._albums[this._albums.length] = purchase;
    }
  }
  this._designs = {};
  for (var i = 0, il = this._albums.length; i < il; i++) {
    var album = this._albums[i];
    
    if (this._designs[album.product.designId] == undefined) {
      this._designs[album.product.designId] = [album];
    } else {
      this._designs[album.product.designId][this._designs[album.product.designId].length] = album;
    }
  }
  this._createElements();
};

EditPurchase.prototype._createElements = function() {
  var unique, _self = this;

  DOMUtils.removeChildElements(this._container);
  
  unique = MiscUtils.unique()
  new EditPurchaseGui(unique, this._container, this._item, { designs: this._designs, samples: this._samples });
  
  this._gui = { product: document.getElementById(unique + 'product'),
                quantity: document.getElementById(unique + 'quantity'),
                price: document.getElementById(unique + 'price'),
                btnDel: document.getElementById(unique + 'btnDel')
              };
  
  
  for (var i = 0, il = this._gui.product.options.length; i < il; i++) {
    option = this._gui.product.options[i];
    option._sample = this._samples[i];
  }
  this._gui.product.onchange = function() { _self._item.product.sampleId = this.options[this.selectedIndex]._sample.id;
                                            _self._item.product.name = this.options[this.selectedIndex]._sample.name;
                                            _self._item.product.price = this.options[this.selectedIndex]._sample.price;
                                            _self._item.product.type = this.options[this.selectedIndex]._sample.type;
                                            _self._item.product.model = this.options[this.selectedIndex]._sample.model;
                                            _self._item.product.designId = MiscUtils.unique();
                                            _self._item.product.selects = [];
                                            for (var i = 0, il = this.options[this.selectedIndex]._sample.attributes.length; i < il; i++) {
                                              var attribute = this.options[this.selectedIndex]._sample.attributes[i];
                                              var select = ProductSelect.instance();
                                              select.sampleAttributeOptionId = attribute.options[0].id;
                                              select.sampleAttributeId = attribute.id;
                                              select.sampleAttributeName = attribute.name;
                                              select.name = attribute.options[0].name;
                                              _self._item.product.selects[_self._item.product.selects.length] = select;
                                            }
                                            _self._item.price = (_self._item.quantity * _self._item.product.price).toFixed(2);
                                            _self._callbackFunc();
                                          };

  this._gui.quantity.onchange = function() { _self._item.quantity = this.value;
                                             _self._item.price = (_self._item.quantity * _self._item.product.price).toFixed(2);
                                             _self._callbackFunc();
                                           };
  
  this._gui.price.onchange = function() { _self._item.price = this.value;
                                          _self._callbackFunc();
                                        };

  this._gui.btnDel.onclick = function() { _self._parent.purchases.splice(_self._index, 1);
                                          _self._callbackFunc();
                                          return false;
                                        };
  
  for (var i = 0, il = DlmanageUtils.sample(this._item.product, this._samples).attributes.length; i < il; i++) {
    var attribute = DlmanageUtils.sample(this._item.product, this._samples).attributes[i];
    for (var j = 0, jl = attribute.options.length; j < jl; j++) {
      var option = attribute.options[j];
      
      var optionTd = document.getElementById(unique + '_' + i + '_' + j + 'optionTd');
      optionTd._attribute = attribute;
      optionTd._option = option;
      optionTd.onclick = function() { _self._addSelect.call(_self, this._attribute, this._option);
                                      _self._callbackFunc();
                                      return false;
                                    };
    }
  }

  if (this._designs[this._item.product.designId]) {
    if (this._designs[this._item.product.designId].length > 1) {
      var btn = document.getElementById(unique + 'btnDesign');
      
      btn.onclick = function() { _self._item.product.designId = MiscUtils.unique();
                                 _self._callbackFunc();
                               };
    }
  
    for (var designId in this._designs) {
      var other = DlmanageUtils.design(this._designs[designId]);
      var self = DlmanageUtils.design([this._item]);
      
      if (other.page < self.page || other.shape != self.shape) {
        continue;
      }
      
      var radio = document.getElementById(unique + designId + 'radioDesign');
      radio._designId = designId;
      radio.onclick = function() { _self._item.product.designId = this._designId;
                                   _self._callbackFunc();
                                 };
    }
  }
};

EditPurchase.prototype._addSelect = function(attribute, option) {
  var i = 0;
  while (i < this._item.product.selects.length) {
    var select = this._item.product.selects[i];
    if (select.sampleAttributeId == attribute.id) {
      this._item.product.selects.splice(i, 1);
      i = 0;
    } else {
      i++;
    }
  }
  var select = ProductSelect.instance();
  select.sampleAttributeOptionId = option.id;
  select.sampleAttributeId = attribute.id;
  select.sampleAttributeName = attribute.name;
  select.name = option.name;
  this._item.product.selects[this._item.product.selects.length] = select;
};